package com.ztsoft.user.service.user.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztsoft.user.mapper.user.SysMenuMapper;
import com.ztsoft.user.pojo.entity.user.SysMenu;
import com.ztsoft.user.pojo.entity.user.SysRoleMenu;
import com.ztsoft.user.pojo.vo.user.SysMenuTree;
import com.ztsoft.user.pojo.vo.user.SysMenuVo;
import com.ztsoft.user.service.user.SysMenuService;
import com.ztsoft.user.service.user.SysRoleMenuService;
import com.ztsoft.user.utils.MenuTreeUtils;
import com.ztsoft.user.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public Result<Object> queryMenuByPage(SysMenu menu, Integer currentPage, Integer pageSize) {
        try {
            Map<String, Object> resultMap = new HashMap<>();

            // 构建查询条件
            QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>(menu);
            queryWrapper.orderByAsc("order_num");

            // 分页查询
            IPage<SysMenu> page = this.page(new Page<>(currentPage, pageSize), queryWrapper);

            // 数据实体转VO
            List<SysMenuVo> list = JSON.parseArray(JSON.toJSONString(page.getRecords()), SysMenuVo.class);

            resultMap.put("count", page.getTotal());
            resultMap.put("data", list);
            return Result.success(resultMap);
        } catch (Exception e) {
            log.info("出现异常" + e);
            return Result.fail();
        }
    }

    @Override
    public Result<Object> queryMenuTreeSelect() {
        // 查询数据库所有数据
        List<SysMenu> list = this.list();
        // 树状菜单存放集合
        List<SysMenuTree> sysMenuTreeList = new ArrayList<>();

        // SysMenu转SysMenuTree
        for (SysMenu menu : list) {
            SysMenuTree sysMenuTree = new SysMenuTree();
            sysMenuTree.setValue(menu.getMenuId());
            sysMenuTree.setLabel(menu.getMenuName());
            sysMenuTree.setParentId(menu.getParentId());
            sysMenuTree.setDisabled(menu.getStatus() == 2);
            sysMenuTreeList.add(sysMenuTree);
        }

        // 构建树状
        List<SysMenuTree> sysMenuTreeInfo = MenuTreeUtils.buildMenuTree(sysMenuTreeList);

        // 树形层级结构中的数量统计
        MenuTreeUtils.getTreeCount(sysMenuTreeInfo);
        return Result.success(sysMenuTreeInfo);
    }

    @Override
    public Result<Object> addMenu(SysMenuVo sysMenuVo) {
        SysMenu menu = BeanUtil.copyProperties(sysMenuVo, SysMenu.class);
        // true=1,false=2
        menu.setHideMenu(Boolean.TRUE.equals(sysMenuVo.getHideMenu()) ? 1 : 2);
        menu.setAlwayShow(Boolean.TRUE.equals(sysMenuVo.getAlwayShow()) ? 1 : 2);
        menu.setStatus(Boolean.TRUE.equals(sysMenuVo.getStatus()) ? 1 : 2);
        boolean isSuccess = this.save(menu);
        if (isSuccess) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> updateMenu(SysMenuVo sysMenuVo) {
        SysMenu menu = BeanUtil.copyProperties(sysMenuVo, SysMenu.class);
        menu.setHideMenu(Boolean.TRUE.equals(sysMenuVo.getHideMenu()) ? 1 : 2);
        menu.setAlwayShow(Boolean.TRUE.equals(sysMenuVo.getAlwayShow()) ? 1 : 2);
        menu.setStatus(Boolean.TRUE.equals(sysMenuVo.getStatus()) ? 1 : 2);
        boolean isSuccess = this.updateById(menu);
        if (isSuccess) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> deleteMenu(String id) {
        QueryWrapper<SysRoleMenu> sysRoleMenuQueryWrapper = new QueryWrapper<>();
        sysRoleMenuQueryWrapper.eq("menu_id", id);
        sysRoleMenuService.remove(sysRoleMenuQueryWrapper);

        boolean isSuccess = this.removeById(id);
        if (isSuccess) {
            return Result.success();
        }
        return Result.fail();
    }

}